import React from 'react';
import { Tree, Card, Button } from 'antd';
import {
    CodepenOutlined,
    ShareAltOutlined,
    SettingOutlined,
    FileOutlined,
} from '@ant-design/icons';
import { connect } from 'umi';
import * as Request from '@/services/Request';

const apiURL = "/api/services/app/PartBOM/";
class BomTree extends React.Component<any, any> {
    constructor(props: any) {
        super(props);
        this.state = {
            loading: false,
            visible: false,
            treeData: []
        }
    }
    componentDidMount() {
        this.props.onRef(this)
        this.fetchData(this.props.BomData)
    }
    fetchData = (BomData: any) => {
        if (BomData && BomData.node) {
            var treeData = []
            var node = {
                title: BomData.node.partNum + BomData.node.partDescription,
                key: BomData.node.id,
                item: BomData.node,
                icon: <CodepenOutlined />,
                type:'root',
                children: BomData.childs ? this.mapData(BomData.childs, BomData.boos) : null
            }
            treeData.push(node)

            this.setState({ treeData })
        }
    }
    mapData = (parentNode: any, boos: any) => {
        var treeData: any = []
        boos.map((item: any) => {
            var node = {
                title: item.node.oprSeq + "-" + item.node.opDesc,
                key: item.node.id,
                item: item.node,
                icon: item.node.subContract?<ShareAltOutlined />:<SettingOutlined />,
                type:'boo',
                children: item.mtls ? this.mapMtlData(item.mtls) : null
            }
            treeData.push(node)
        });
        parentNode.map((item: any) => {
            var node = {
                title: item.node.partNum + item.node.partDescription,
                key: item.node.id,
                item: item.node,
                icon: <CodepenOutlined />,
                type:'asm',
                children: item.childs ? this.mapData(item.childs, item.boos) : null
            }
            treeData.push(node)
        });

        return treeData;
    }
    mapMtlData = (mtls: any) => {
        var treeData: any = []
        mtls.map((item: any) => {
            var node = {
                title: item.partNum + item.partDescription,
                key: item.id,
                item,
                icon: <FileOutlined />,
                type:'mtl',
            }
            treeData.push(node)
        });
        return treeData;
    }
    render() {
        const { treeData } = this.state;
        const onExpand = (expandedKeys: any) => {
            //console.log('onExpand', expandedKeys);
        };

        const onCheck = (checkedKeys: any) => {
            //console.log('onCheck', checkedKeys);
        };

        const onSelect = (selectedKeys: any, info: any) => {
            this.props.setCurrentAsmb(info.node.item,info.node.type)
        };
        return (
            <Card title="制造工艺">
                <Tree
                    showIcon
                    checkable
                    defaultExpandAll
                    onExpand={onExpand}
                    onCheck={onCheck}
                    onSelect={onSelect}
                    treeData={treeData}
                />
            </Card>
        );
    }
}

export default connect(({ Abp }: API.IConnectState) => ({
    response: Abp.response,
    currentUser: Abp.currentUser,
}))(BomTree);